﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using ProjBackEnd.DAL;
using ProjBackEnd.Lib;
using ProjBackEnd.Areas.Sys.Models;
using ProjBackEnd.DBInstance;

namespace ProjBackEnd.Areas.Sys.Controllers
{
    /// <summary>
    /// 用户登录操作
    /// </summary>
    public class UserLoginController : ApiController
    {
        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="_reqInfo">用户登录请求</param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/sys/userlogin")]
        public RespUserLoginInfo UserLogin(ReqUserLoginInfo _reqInfo)
        {
            RespUserLoginInfo resp = new RespUserLoginInfo();

            if (!ModelState.IsValid)
            {
                string strErrmsg = DataUtil.GetModelStateError(ModelState);
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误。" + strErrmsg;
                return resp;
            }
            bool isAutoLogin = true;
            if (_reqInfo.logintp.ToUpper() == "M") isAutoLogin = false;

            UserInfo_LoginSucc collecUser = userLogic.CheckMember(_reqInfo.loginid, _reqInfo.password, isAutoLogin);
            if (collecUser == default(UserInfo))
            {
                resp.statusCode = 10002;
                resp.statusMsg = "用户名或密码错误";
                return resp;
            }

            string strToken = "";
            int iRet = userLogic.GetToken(collecUser.memberid, _reqInfo.platcd, out strToken);

            resp.statusCode = iRet;
            switch (iRet)
            {
                case 0:
                    if (strToken == "")
                    {
                        resp.statusCode = 10003;
                        resp.statusMsg = "获取Token失败";
                        return resp;
                    }
                    List<VIEW_SYS_USERPERMISSION> lstPms = userLogic.GetUserPermission(collecUser.memberid, _reqInfo.comcd);
                    if (lstPms.Count == 0)
                    {
                        resp.statusCode = 60001;
                        resp.statusMsg = "用户权限获取失败";
                        return resp;
                    }

                    List<Menuinfo> lstMenuInfo = new List<Menuinfo>();
                    foreach (VIEW_SYS_USERPERMISSION _collec in lstPms)
                    {
                        Menuinfo menuInfo = new Menuinfo();
                        menuInfo.menuid = _collec.menuid;
                        menuInfo.menucd = _collec.menucd;
                        menuInfo.pmenuid = _collec.pmenuid;
                        menuInfo.menunmchg = _collec.menunmchg;
                        menuInfo.menunmeng = _collec.menunmeng;
                        menuInfo.formnm = _collec.formnm;
                        menuInfo.menuorder = _collec.menuorder;
                        menuInfo.opentp = _collec.opentp;
                        menuInfo.iconurl = _collec.iconurl;
                        lstMenuInfo.Add(menuInfo);
                    }

                    ResultUserLoginInfo res = new ResultUserLoginInfo();
                    res.username = collecUser.username;
                    res.memberid = collecUser.memberid;
                    res.avatar = collecUser.avatar;
                    res.deptcd = collecUser.deptcd;
                    res.mpno = collecUser.mpno;
                    res.token = DataUtil.PlaintextConfusion(strToken, DataUtil.enumDeEncryptType.Encrypt); //加密token
                    res.menuinfo = lstMenuInfo;
                    res.deptcd = collecUser.deptcd;

                    resp.result = res;
                    resp.statusMsg = "登录成功";
                    break;
                case 90005:
                    resp.statusMsg = "数据访问异常";
                    break;
                default:
                    resp.statusMsg = "未知错误";
                    break;
            }

            return resp;
        }
    }
}
